//
//  UnincorporateMsgVM.swift
//  kxf
//
//  Created by Elavatine on 2024/10/8.
//


import Foundation
import UIKit
import SwiftUI

class UnincorporateMsgVM: UIView {
    
    var selfHeight = RegisterInputVM().selfHeight*7
    
    var controller = WHBaseViewVC()
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var idNameVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.leftLabel.text = "非法人姓名"
        vm.textField.placeholder = "请输入法人姓名"
        vm.textField.keyboardType = .default
        vm.maxLength = 30
        return vm
    }()
    lazy var idNoVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idNameVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "非法人证件号码"
        vm.textField.placeholder = "请输入证件号码"
//        vm.textField.keyboardType = .default
        vm.maxLength = 30
        return vm
    }()
    lazy var idStartTimeVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idNoVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "非法人证件有效期始"
        vm.textField.placeholder = "请选择证件有效期始"
        vm.showArrowImg()
        vm.tapBlock = {()in
            self.choiceTimeAction()
        }
        return vm
    }()
    lazy var idEndTimeVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idStartTimeVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "非法人证件有效期至"
        vm.textField.placeholder = "请选择证件有效期至"
        vm.showArrowImg()
        vm.tapBlock = {()in
            self.choiceTimeEndAction()
        }
        return vm
    }()
//    lazy var bankPrivateNameVm : RegisterInputVM = {
//        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idEndTimeVm.frame.maxY, width: 0, height: 0))
//        vm.leftLabel.text = "非法人银行名称"
//        vm.textField.placeholder = "请填写银行名称"
//        vm.textField.keyboardType = .default
//        return vm
//    }()
    lazy var bankNoVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.idEndTimeVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "非法人银行卡号"
        vm.textField.placeholder = "请输入银行卡号"
//        vm.textField.keyboardType = .default
        vm.textField.keyboardType = .numberPad
        vm.maxLength = 30
        return vm
    }()
    lazy var bankPrivateBranchVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankNoVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "非法人开户支行"
        vm.textField.placeholder = "请选择开户支行"
        vm.showArrowImg()
        vm.tapBlock = {()in
            let vc = AuthenBankBranchListVC()
            vc.tapBlock = {(dict)in
                AuthenCompanyMsgModel.shareInstance.unincorporateBankNo = "\(dict["branchNo"]as? String ?? "")"
                AuthenCompanyMsgModel.shareInstance.unincorporateBankName = "\(dict["branchBankName"]as? String ?? "")"
                self.bankPrivateBranchVm.setLabelContent(content: AuthenCompanyMsgModel.shareInstance.unincorporateBankName)
            }
            self.controller.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var bankPhoneVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankPrivateBranchVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "非法人预留手机号"
        vm.textField.placeholder = "请输入预留手机号"
        vm.textField.keyboardType = .numberPad
        vm.maxLength = 11
        return vm
    }()
    lazy var datePickerVm : DatePickerVM = {
        let vm = DatePickerVM.init(frame: .zero)
        vm.isHidden = true
        
        return vm
    }()
}

extension UnincorporateMsgVM{
    func initUI() {
        addSubview(idNameVm)
        addSubview(idNoVm)
        addSubview(idStartTimeVm)
        addSubview(idEndTimeVm)
        addSubview(bankNoVm)
//        addSubview(bankPrivateNameVm)
        addSubview(bankPrivateBranchVm)
        
        addSubview(bankPhoneVm)
        UIApplication.shared.keyWindow?.addSubview(datePickerVm)
    }
    func updateUIForRefuse() {
        idNameVm.textField.text = "\(AuthenCompanyMsgModel.shareInstance.unincorporateIdCardName)"
        idNoVm.textField.text = "\(AuthenCompanyMsgModel.shareInstance.unincorporateIdCardNo)"
        idStartTimeVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.unincorporateIdCardStartDate)")
        idEndTimeVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.unincorporateIdCardEndDate)")
//        bankPrivateNameVm.textField.text = "\(AuthenCompanyMsgModel.shareInstance.unincorporateBankName)"
        bankNoVm.textField.text = "\(AuthenCompanyMsgModel.shareInstance.unincorporateCardNo)"
//        bankPrivateNameVm.textField.text = "\(AuthenCompanyMsgModel.shareInstance.unincorporateBankName)"
        bankPrivateBranchVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.unincorporateBankName)")
        bankPhoneVm.textField.text = "\(AuthenCompanyMsgModel.shareInstance.unincorporateBankTel)"
    }
}

extension UnincorporateMsgVM{
    func choiceTimeAction(){
        self.datePickerVm.isHidden = false
        self.datePickerVm.titleLabel.text = "证件有效期始"
        self.datePickerVm.timeConfirmBlock = {(time)in
            AuthenMiniMsgModel.shareInstance.legalIdCardStartTime = time
//            self.idStartTimeVm.textField.text = time
            self.idStartTimeVm.setLabelContent(content: time)
            self.choiceTimeEndAction()
        }
    }
    func choiceTimeEndAction(){
        let alertVc = UIAlertController(title: "身份证是否长期有效", message: "", preferredStyle: .alert)
        
        let okAction = UIAlertAction(title: "长期有效", style: .default, handler: { (action)in
            AuthenMiniMsgModel.shareInstance.legalIdCardEndTime = "9999-01-01"
            self.idEndTimeVm.setLabelContent(content: AuthenMiniMsgModel.shareInstance.legalIdCardEndTime)
        })
        alertVc.addAction(okAction)
        let cancelAction = UIAlertAction(title: "非长期", style: .cancel, handler:{ (action)in
            self.datePickerVm.isHidden = false
            self.datePickerVm.titleLabel.text = "证件有效期至"
            self.datePickerVm.timeConfirmBlock = {(time)in
                AuthenMiniMsgModel.shareInstance.legalIdCardEndTime = time
//                self.idEndTimeVm.textField.text = AuthenMiniMsgModel.shareInstance.legalIdCardEndTime
                self.idEndTimeVm.setLabelContent(content: AuthenMiniMsgModel.shareInstance.legalIdCardEndTime)
            }
        })
        alertVc.addAction(cancelAction)
        self.controller.present(alertVc, animated:true, completion:nil)
    }
}
